#!/usr/bin/env python
# -*- coding: utf-8 -*- 
# Author: caojianfeng
# E-mail: windcao@hotmail.com
import os
import os.path

input_dir = "input/"
output_dir = "output/"
def findInputFiles(root_input_dir):
    found_files = []
    for parent, dirnames, filenames in os.walk(root_input_dir):
        for filename in filenames:
            if filename.endswith(".apk"):
                found_files.append(filename)
    return found_files

def revert_apk(in_dir,apk_file,out_dir):
    in_file = os.path.join(in_dir,apk_file)
    out_file = os.path.join(out_dir,apk_file.replace(".","_"))+"_smali"
    cmd = "sh libs/apktool d %s -f -o %s"%(in_file,out_file)
    print cmd
    os.system(cmd)
    out_fileD2j = os.path.join(out_dir,apk_file.replace(".","_"))+".jar"
    cmdD2j = "sh libs/dex2jar/d2j-dex2jar.sh %s -f -o %s" %(in_file,out_fileD2j)
    print cmdD2j
    os.system(cmdD2j)
# -- main ---
if __name__ == '__main__':
    apk_files = findInputFiles(input_dir)
    for apk_file in apk_files:
        revert_apk(input_dir,apk_file,output_dir)